<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_wxapps';
$return=array();
if($_RQ['op']=='get_wxapps'){
	//小程序
	$type=intval($_RQ['type']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>$type);//应用小程序
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$fields=array('id','title','type','version','plugin_name','isdiyindex');
	$pindex = max(1, $_RQ['page']);
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_wxapp',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['plugin_title']=pdo_getcolumn('core_plugins',array('name'=>$item['plugin_name']),'title');
		$item['plugin_title']=$item['plugin_title']?:'DIY/打包小程序';
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('action'=>'jy_weishop/activity.live.get_data/get_wxapps/type=2','title'=>'应用小程序列表'),
			array('action'=>'jy_weishop/activity.live.get_data/get_wxapps/type=8','title'=>'DIY小程序列表'),
			array('action'=>'jy_weishop/activity.live.get_data/get_wxapps/type=7','title'=>'打包小程序列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('type'=>'string','title'=>'小程序名称','name'=>'title'),
			array('type'=>'string','title'=>'所属应用','name'=>'plugin_title'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'id','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='get_rooms'){
	$rooms=array();	
	$live_id=intval($_RQ['live_id']);
	$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$live_id),'wxapp_id');
	if(!$wxapp_id){
		exi('没有获取到小程序ID，请确认绑定的小程序是否存在！','error');
	}
	for ($i=0; $i<=400; $i+50){
		$data=array('start'=>$i,'limit'=>50);
		$res=model('core/wxapp_api',$wxapp_id)->get_live_rooms($data);
		$res=json_decode($res,true);
		if($res['errcode']==0){
			$res['Response']=json_decode($res['Response'],true);
			$plus=$res['Response'];
			$rooms=array_merge($rooms,$plus);
			if(count($plus)<50){
				 break;
			}
		}else{
			 break;
		}
	}
	if(count($rooms)>0){
		foreach($rooms as &$item){
			$id=pdo_getcolumn('core_account_wxapp_live_rooms',array('live_id'=>$live_id,'roomid'=>$item['roomid']),'id');
			$data=array(
				'name'=>$item['name'],
				'roomid'=>$item['roomid'],
				'cover_img'=>$item['cover_img'],
				'live_satus'=>$item['live_satus'],
				'start_time'=>$item['start_time'],
				'end_time'=>$item['end_time'],
				'anchor_name'=>$item['anchor_name'],
				'anchor_img'=>$item['anchor_img'],
				'total'=>$item['total'],
			);
			if($id){
				pdo_update('core_account_wxapp_live_rooms',$data,array('id'=>$id));
			}else{
				$data['createtime']=TIMESTAMP;
				$data['live_id']=$live_id;
				$data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('core_account_wxapp_live_rooms',$data);
			}
		}
		exi('同步房间成功！');
	}else{
		exi('同步房间失败！小程序官方没有创建直播间！','error');
	}
}
if($_RQ['op']=='get_goods'){
	//已审核商品列表
	$live_id=$this->session('live_id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'live_id'=>$live_id,'status'=>2);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['roomsid']){
		$params['roomsid']=$_RQ['roomsid'];
		$return['params']['roomsid']=$_RQ['roomsid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('core_account_wxapp_live_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$goods_ids=array();
		$sale_live=$this->plugin_setting('sale_live');
		$install = check_account_permission('jy_commission');
		foreach($list as &$item){
			$item['image']=$item['image']?tomedia($item['image']):'';
			if($_RQ['op']=='many' || $_RQ['getinfo']){
				if($_RQ['getinfo']){
					$item['res']=array(
						'title'=>$item['title'],
						'id'=>$item['id'],
						'appgoodsid'=>$item['appgoodsid'],
						'image'=>$item['image'],
						'price'=>$item['price'],
						'originalprice'=>$item['originalprice'],
						'salenum'=>$item['salenum'],
					);
				}else{
					$item['res']=array(
						'title'=>$item['title'],
						'id'=>$item['id'],
						'appgoodsid'=>$item['appgoodsid'],
						'image'=>$item['image'],
					);
				}
			}else{
				$item['res']=$item['id'];
			}
			if($install){
					$commfee=$this->plugin('jy_commission')->get_active($item['goods_id']);
			}
			$item['getfee']=$commfee['commission_money']?:0;
			if($sale_live['openticheng'] && $sale_live['tichengrate']){
					$item['getfee'] +=$item['price']*$sale_live['tichengrate']/100;
			}
			$item['getfee']=price($item['getfee']);
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='get_coupons'){
	//已审核商品列表
	$live_id=$this->session('live_id');
	$sale_live=$this->plugin_setting('sale_live');
	if($sale_live['couponids']){
		$list = str_array($sale_live['couponids']);
		foreach($list as &$item){
			$item=$this->model('coupon')->intcoupon($item);
			$item['sendtime']=json_decode($item['sendtime'],true);
			$item['etime']=$item['sendtime']['tend'];
			if($item['sendtime']['tstart']>time()){
				$item['issend']=0;
				$item['etime']=$item['sendtime']['tstart'];
			}elseif($item['sendtime']['tstart']<time() && $item['sendtime']['tend']>time()){
				$item['issend']=1;
				$item['etime']=$item['sendtime']['tend'];
			}elseif($item['sendtime']['tend']<time()){
				$item['issend']=2;
			}
			$item['sendtime']['tend2']=$item['sendtime']['tend'];
			$item['sendtime']['tstart2']=$item['sendtime']['tstart'];
			$item['sendtime']['tend']=date('Y-m-d',$item['sendtime']['tend']);
			$item['sendtime']['tstart']=date('Y-m-d',$item['sendtime']['tstart']);
			if($item['timelimittype']==2 && $limittimes2['tend']<time()){
				unset($getcoupon[$k]);
			}else{
				$coupon[]=$item;
			}
		}
	}
	$return['list']=$list?:array();
	exi($return);
}
if($_RQ['op']=='get_feemodal'){
	//订单收入
	$live_id=$this->session('live_id');
	$sale_live=$this->plugin_setting('sale_live');
	if($sale_live['openticheng'] && $sale_live['tichengrate']){
		$roomids=pdo_getall('core_account_wxapp_live_rooms',array('mid'=>$_SESSION['uid'],'live_id'=>$live_id,'uniacid'=>$_SESSION['uniacid']),'id','id');
		if($roomids){
			$roomids=array_keys($roomids);
		}else{
			$return['list']=$list?:array();
			exi($return);
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'roomid'=>$roomids,'ispay'=>1,'status <>'=>4);
		if(!empty($_RQ['title'])){
			$params['title like']='%'.trim($_RQ['title']).'%';
			$return['params']['title']=$_RQ['title'];
		}
		$pindex = max(1, intval($_RQ['page']));
		$psize = 20;
		$total='';
		$list=pdo_getslice('jy_weishop_order_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
		if($list){
			foreach($list as &$item){
				$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']),array('title','image'));
				$goods['image']=$goods['image']?tomedia($goods['image']):'';
				$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
				$item['fee']=price($item['total_price']*$sale_live['tichengrate']/100);
				$item=array_merge($item,$goods);
			}	
		}
	}
	$return['list']=$list?:array();
	exi($return);
}